package com.liu.project.service.impl;

import com.liu.project.dao.UserMapper;
import com.liu.project.pojo.User;
import com.liu.project.pojo.UserInfo;
import com.liu.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @program: project
 * @description: 用户操作类
 * @author: 86187
 * @create: 2022-01-05 15:10
 **/


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public UserInfo findUserDetailById(int id) {
        return userMapper.findUserDetailById(id);
    }

    @Override
    public User findUserByUserName(String username) {
            return userMapper.findUserByUsername(username);
    }

    @Override
    public User findUserByEmail(String email) {
        return userMapper.findUsersByEmail(email);
    }

    @Override
    public User findUserByUsernameAndPassword(String username, String password) {
        return userMapper.findUserByUsernameAndPassword(username,password);
    }

    @Override
    public void addUser(String username,String password,String role,int detailId) {
        User user = new User();
        String encode = new BCryptPasswordEncoder().encode(password);//加密存储
        user.setUsername(username);
        user.setPassword(encode);
        user.setRole(role);
        user.setDetailId(detailId);
            userMapper.addUser(user);
        }
    @Override
    public User findUsersByUserId(int userId) {
      return userMapper.findUsersByUserId(userId);
    }

    @Override
    public boolean updateUserDetail(UserInfo userInfo) {
        int i = userMapper.updateUserDetail(userInfo);
        return i>0;

    }

    @Override
    public boolean updatePassword(int id, String newPassword) {
        int i = userMapper.updatePassword(id, newPassword);
        return i>0;
    }

    @Override
    public boolean updateHeader(int id, String url) {
        int i = userMapper.updateHeader(id, url);
        return i>0;
    }

}



